package drds.global_transaction.lock;

import org.feisoft.common.utils.DbPool.Db;
import org.feisoft.common.utils.SpringBeanUtil;

import java.sql.SQLException;

/*
    插入共享锁如果之前
    未锁：初始一把共享锁
    已有互斥锁：获取锁失败
    已有共享锁：共享队列+1，获取锁成功
 */
public class ShareILock extends Lock
{

    private Db db = null;

    {
        if (this.db == null)
        {
            Db db = (Db) SpringBeanUtil.getBean("db");
            this.db = db;
        }
    }

    @Override
    public void lock() throws SQLException
    {
        String sql = "select count(0) as total from  txc_lock where  table_name = '" + tableName + "' and key_value ="
                + primaryKeyColumnValue + " and exclusiveLock='" + exclusiveLock + "'";
        int total = db.countList(sql);
        if (total == 0)
        {
            insertLock();
            insertShareLock();
        } else if (total == 1)
        {
            throw new SQLException();
        } else
        {
            insertShareLock();
        }

        setLock(Boolean.TRUE);

    }

    @Override
    public void unlock() throws SQLException
    {
        deleteLock();
        setLock(Boolean.FALSE);
    }

    private void insertShareLock() throws SQLException
    {
        String sql = "insert into txc_lock (table_name,key_value,gloableTransactionId,branch_id,exclusiveLock,shareLock,create_time)values(";
        sql += "'" + tableName + "'," + primaryKeyColumnValue + ",'" + gloableTransactionId + "','" + branchTransactionId + "','" + gloableTransactionId + "'," + shareLock + ","
                + createDateTimeLong;
        sql += ")";
        db.executeUpdate(sql);
    }

    ;
}
